# -*- coding: utf8 -*-
import logging
import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from book import booking
from book import mybooking
from book import shop
from book import today
from book import report
from book import user
from book import balance

class RestfulMethodMiddleware(object):
    """
    RESTful中间件
    """
    def __init__(self, app):  
        self.app = app  
   
    def __call__(self, environ, start_response):  
        method = webapp.Request(environ).get('_method')
        if method:
            environ['REQUEST_METHOD'] = method.upper()
            logging.getLogger().debug(u'REQUEST_METHOD: %s' % method.upper());
        return self.app(environ, start_response)  

def main():
    logging.getLogger().setLevel(logging.DEBUG)

    application = webapp.WSGIApplication([(r'/', booking.MainPage),
                                          (r'/booking', booking.MainPage),
                                          (r'/booking/dish/(\w+)', booking.DishPage),
                                          (r'/mybooking', mybooking.OrderHomePage),
                                          (r'/mybooking/list/(\w+)', mybooking.OrderListPage),
                                          (r'/mybooking/del/(\w+)/(\w+)', mybooking.OrderCancelPage),
                                          (r'/mybooking/(\w+)', mybooking.OrderPage),
                                          (r'/shop', shop.ShopPage),
                                          (r'/shop/del/(\w+)', shop.ShopPage),
                                          (r'/dish/list/(\w+)', shop.DishListPage),
                                          (r'/dish/(\w+)', shop.DishPage),
                                          (r'/dish/(\w+)/(\w+)', shop.DishPage),
                                          (r'/shop/list', shop.ShopListPage),
                                          (r'/user', user.UserPage),
                                          (r'/user/list', user.UserPage),
                                          (r'/user/del/(\w+)', user.UserPage),
                                          (r'/user/(\w+)', booking.UserPage),
                                          (r'/today', today.TodayOrderPage),
                                          (r'/balance', balance.BalancePage),
                                          (r'/report', report.ReportHomePage)],
                                        debug=True)
    
    wsgiref.handlers.CGIHandler().run(RestfulMethodMiddleware(application))

if __name__ == "__main__":
    main()